package ServletCooking;

/**
 * @author 李某
 * * @date 2021/11/20
 * *物有本末，事有终始。知其先后者，则近道矣！
 * *荷花开满池塘的前一天还是半塘荷花半塘云。
 */


import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

    /**
     * Servlet implementation class ReadCookies
     */
//    @WebServlet("/ReadCookies")
    public class ReadCookies extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public ReadCookies() {
            super();
            // TODO Auto-generated constructor stub
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            Cookie cookie = null;
            Cookie[] cookies = null;
            // 获取与该域相关的 Cookie 的数组
            cookies = request.getCookies();

            // 设置响应内容类型
            response.setContentType("text/html;charset=UTF-8");

            PrintWriter out = response.getWriter();
            String title = "Delete Cookie Example";
            String docType = "<!DOCTYPE html>\n";
            out.println(docType +
                    "<html>\n" +
                    "<head><title>" + title + "</title></head>\n" +
                    "<body bgcolor=\"#f0f0f0\">\n" );
            if( cookies != null ){
                out.println("<h2>Cookie 名称和值</h2>");
                for (int i = 0; i < cookies.length; i++){
                    cookie = cookies[i];
                    if((cookie.getName( )).compareTo("name") == 0 ){
                        cookie.setMaxAge(0);
                        response.addCookie(cookie);
                        out.print("已删除的 cookie：" +
                                cookie.getName( ) + "<br/>");
                    }
                    out.print("名称：" + cookie.getName( ) + "，");
                    out.print("值：" +  URLDecoder.decode(cookie.getValue(), "utf-8") +" <br/>");
                }
            }else{
                out.println(
                        "<h2 class=\"tutheader\">No Cookie founds</h2>");
            }
            out.println("</body>");
            out.println("</html>");
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }

    }

/**
 * 编译上面的 Servlet **ReadCookies**，并在 web.xml 文件中创建适当的条目。
 * 尝试运行 *http://localhost:8080/TomcatTest/ReadCookies*，
 */

